package pl.register.android.activities.profile.logic;

import com.google.inject.Singleton;
import pl.register.android.activities.profile.ui.StudentProfileView;
import pl.register.android.course.Course;
import pl.register.android.database.exception.NotFoundGroupException;
import pl.register.android.classes.Classes;
import pl.register.android.database.DatabaseAdapter;
import pl.register.android.database.dao.*;
import pl.register.android.group.StudentsGroup;
import pl.register.android.homework.Homework;
import pl.register.android.lesson.Lesson;
import pl.register.android.lesson.TagLesson;
import pl.register.android.program.Program;
import pl.register.android.student.Student;
import pl.register.android.year.Year;
import pl.register.android.year.YearType;

import java.util.*;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-20
 */

@Singleton
public class StudentProfilePresenter {

    private StudentProfileView profileView;

    private CourseDao courseDao;

    private YearDao yearDao;

    private StudentsDao studentsDao;

    private StudentGroupDao groupDao;

    private ProgramDao programDao;


    private StudentLessonDao lessonDao;

    private Course course;

    private Year year;

    private StudentsGroup group;

    private Student student;

    public void init(Student student, DatabaseAdapter databaseAdapter, StudentProfileView profileView) {
        this.student = student;
        this.profileView = profileView;
        courseDao = databaseAdapter.getCourseDao();
        programDao = databaseAdapter.getProgramDao();
        groupDao = databaseAdapter.getStudentGroupDao();
        yearDao = databaseAdapter.getStudentYearDao();
        studentsDao = databaseAdapter.getStudentDao();
        lessonDao = databaseAdapter.getStudentLessonDao();
    }

    public void findCourse(Long courseId) {
        course = courseDao.findCourseBy(courseId);
    }

    public YearType getYearType() {
        return year.getType();
    }

    public void findStudentYear(Long yearId) {
        year = yearDao.findYearsBy(yearId);
        Program program = programDao.findProgramFor(year);
        year.setProgram(program);
    }

    public void findStudentGroup() throws NotFoundGroupException {
        group = groupDao.findGroupsBy(student, year);
        if (group == null) {
            throw new NotFoundGroupException();
        }
    }

    public List<TagLesson> getTagsBy(YearType yearType) {
        List<TagLesson> tags = new ArrayList<TagLesson>();
        if (yearType.equals(YearType.HOLIDAY_I_YEAR)) {
            tags.add(TagLesson.HOLIDAY_I);
        } else if (yearType.equals(YearType.HOLIDAY_II_YEAR)) {
            tags.add(TagLesson.HOLIDAY_II);
        } else {
            tags = TagLesson.asList();
        }
        return tags;
    }

    public Map<TagLesson, List<Lesson>> createLessonMap() {
        Map<TagLesson, List<Lesson>> map = new HashMap<TagLesson, List<Lesson>>();
        List<TagLesson> tags = TagLesson.asList();
        for (TagLesson tag : tags) {
            Program program = programDao.findProgramForGroupBy(group, tag);
            List<Lesson> lessons = lessonDao.findLessonsFor(student, program);
            map.put(tag, lessons);
        }
        return map;
    }

    public void saveClasses(Classes classes) {
        lessonDao.updateStudentClasses(classes);
    }

    public void saveHomework(Homework homework) {
        lessonDao.updateStudentHomework(homework);
    }

    public void saveStudent(Student student) {
        studentsDao.update(student, course);
        profileView.actualizeStudentDetails(student);
    }

    public void deleteStudent(Student student) {
        if (group != null) {
            groupDao.deleteStudentFrom(student, group);
        }
        lessonDao.deleteLessonsFrom(student, year.getProgram());
        studentsDao.deleteFrom(student, year);
        profileView.finishActivity();
    }
}
